package com.cs.base.lib;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class GlideUtil {

    /**
     * 加载图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImage(Context context, String url, ImageView imageView,int defImg) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .dontTransform()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defImg) //设置占位图，在加载之前显示
                .error(defImg) //在图像加载失败时显示
                .fallback(defImg)
                .into(imageView);
    }

    /**
     * 下载Bitmap图片
     * @param context
     * @param url
     */
    public static void loadBitmap(Context context, String url,LoadListener loadListener) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .dontTransform()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        if(loadListener!=null){
                            loadListener.onResource(resource);
                        }
                    }
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) { }
                });
    }

    interface LoadListener{
        void onResource(Bitmap resource);
    }

    /**
     * 加载圆形图片
     * @param url
     * @param imageView
     */
    public static void loadCircleImage(Context context, String url, ImageView imageView,int defImg) {
        Glide.with(context)
                .load(url)
                .apply(RequestOptions.circleCropTransform())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defImg) //设置占位图，在加载之前显示
                .error(defImg) //在图像加载失败时显示
                .fallback(defImg)
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * @param context
     * @param url
     * @param imageView
     * @param defImg
     */
    public static void loadFilletImage(Context context, String url, ImageView imageView,int defImg,int corner) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defImg) //设置占位图，在加载之前显示
                .error(defImg) //在图像加载失败时显示
                .fallback(defImg)
                .transform(new RoundedCorners(20))
                .into(imageView);
    }

    /**
     * 加载圆角图片背景
     * @param url
     * @param imageView
     */
    public static void loadRoundCornerBg(Context context, String url, ImageView imageView,int defImg, int corner) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .dontTransform()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defImg) //设置占位图，在加载之前显示
                .error(defImg) //在图像加载失败时显示
                .fallback(defImg)
                .transform(new CenterCrop(), new RoundedCorners(corner))
                .into(imageView);
    }
}
